<?php

namespace app\common\service;

use app\admin\model\KnscyInfos;

class GoodService
{


    public static function format_goods(&$item)
    {
        //封面图全路径
        $item->logo = get_full_url($item->logo);
        //获取门票的最低价和最高价
        $item->min_price = 0;
        $item->price = 0;
        $item->max_price = 0;
        $item->ticket_price = [];
        //如果商品有门票
        if (!empty($item->relate_ticket_ids)) {
            $ticket_ids = explode(',', trim($item->relate_ticket_ids, ','));
            $ticket_ids = array_unique($ticket_ids);
            $prices = TicketPriceService::getTicketTodayPrice($ticket_ids);
            $arr = array_unique(array_values($prices));
            $min_price = !empty($arr) ? min($arr) : 0;
            $max_price = !empty($arr) ? max($arr) : 0;

            $item->ticket_price = $prices;
            if ($max_price != $min_price) {
                $item->min_price = $min_price;
                $item->max_price = $max_price;
            } else {
                $item->price = $min_price;
            }
            if ($item->min_price) {
                $item->price_label = $item->min_price;
                $item->price_prefx = '起';
            } else {
                $item->price_label = $item->price;
                $item->price_prefx = '';
            }
        }
        $item->virtual_sales = format_visual_sales($item->virtual_sales);
        //选择购买的开始时间
        $day = date('Y-m-d');
        if (strtotime($day . " 16:00:00") < time()) {
            $day = date('Y-m-d', strtotime("+1 day"));
        }
        $item['start_select_day'] = $day;
    }

    /**
     * 获得门票关联的文案列表
     */
    public static function ticketInfos($relate_remark)
    {
        $remark = json_decode(htmlspecialchars_decode($relate_remark), true);
        if (empty($remark)) {
            return [];
        }
        $info_ids = array_column($remark, 'id');
        $infos = KnscyInfos::whereIn('id', $info_ids)->column('id,content', 'id');

        foreach ($remark as &$v) {

            $v['content'] = isset($infos[$v['id']]) ? $infos[$v['id']]['content'] : '';
            $v['content'] = htmlspecialchars_decode($v['content']);

        }
        usort($remark, function ($a, $b) {
            return $a['sort'] - $b['sort'];
        });
        return $remark;
    }
}